package com.blb.dao.impl;

import com.blb.dao.IPetDao;
import com.blb.pojo.Page;
import com.blb.pojo.Pet;
import com.blb.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PetDaoImpl implements IPetDao {

    //修改宠物信息
    @Override
    public void update(Pet pet) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection, "UPDATE t_pet SET `name`=?, type=?, age=?, sex=? WHERE id = ?",
                pet.getName(),pet.getType(),pet.getAge(),pet.getSex(),pet.getId()).executeUpdate();
        connection.close();
    }

    //根据宠物id获取单个宠物信息
    @Override
    public Pet getPetById(int id) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        Pet pet = JDBCUtils.getOne(connection, Pet.class, "select * from t_pet where id = ?", id);
        connection.close();
        return pet;
    }

    @Override
    public List<Pet> selectPageByName(String name, int size, int page) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql="SELECT * FROM t_pet  ";
        ArrayList<Object> params = new ArrayList<>();
        if (name != null && !name.isEmpty()) {
            sql +=" where name like ? ";
            params.add("%" + name + "%");
        }
        sql += " limit ?,?";
        params.add((page - 1) * size);
        params.add(size);

        List<Pet> list = JDBCUtils.getList(connection, Pet.class, sql, params.toArray());
        connection.close();
        return list;
    }

    @Override
    public int count(String name) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select count(id) count from t_pet ";
        ArrayList<Object> params = new ArrayList<>();

        if (name != null && !name.isEmpty()) {
            sql += " where name like ?";
            params.add("%" + name + "%");
        }

        ResultSet resultSet = JDBCUtils.prepareStatement(connection, sql, params.toArray()).executeQuery();
        resultSet.next();
        int count = resultSet.getInt("count");
        connection.close();
        return count;
    }
    @Override
    public void delete(int id) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection,"delete from t_pet where id=?",id).executeUpdate();
        connection.close();
    }

    @Override
    public Pet selectById(Pet pet) throws Exception{
        Connection connection = JDBCUtils.getConnection();
        Pet one = JDBCUtils.getOne(connection, Pet.class, "select * form t_pet where id=?", pet.getId());
        connection.close();
        return one;
    }

    @Override
    public void insertPet(Pet pet) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "INSERT into t_pet(`name`,type,age,sex) VALUES(?,?,?,?)";
        JDBCUtils.prepareStatement(connection, sql, pet.getName(), pet.getType(), pet.getAge(), pet.getSex()).executeUpdate();
        connection.close();
    }
}
